/*
 * adf.cpp
 *
 *  Created on: 08.06.2012
 *      Author: Roma Jam
 */


#include "stm32f10x_gpio.h"
#include "kl_lib.h"
#include "adf.h"
#include "stm32f10x_adf.h"

adf_t Adf;
AdfDescriptions_t *Adf1424Mhz , *Adf248MHz;

void adf_t::Task(void) {
    if (Delay.Elapsed(&ATimer, 702)) {
        if (LOCKDETECT(Adf1424Mhz) && !LD_1424MHz_Yes) {
            CmdUnit.Printf("Lock Detected: %uMHz \r\n", Adf1424Mhz->Freq);
            LD_1424MHz_Yes = true;
        }
        else if (!LOCKDETECT(Adf1424Mhz) && LD_1424MHz_Yes) {
            CmdUnit.Printf("Lock Released: %uMHz \r\n", Adf1424Mhz->Freq);
            LD_1424MHz_Yes = false;
        }
        if (LOCKDETECT(Adf248MHz) && !LD_248MHz_Yes) {
            CmdUnit.Printf("Lock Detected: %uMHz\r\n", Adf248MHz->Freq);
            LD_248MHz_Yes = true;
        }
        else if (!LOCKDETECT(Adf248MHz) && LD_248MHz_Yes) {
            CmdUnit.Printf("Lock Released: %uMHz\r\n", Adf248MHz->Freq);
            LD_248MHz_Yes = false;
        }
    }
}


void adf_t::HardwareInit(AdfDescriptions_t *PAdf) {
// Software spi interface, Master mode
    RCC_APB2PeriphClockCmd(PAdf->RCC_CLK, ENABLE);
    GPIO_InitTypeDef  GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Pin = PAdf->SCK | PAdf->MOSI | PAdf->CS;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(PAdf->PORT, &GPIO_InitStructure);
    ADF_CS_HI(PAdf);


// Enable LOCKDETECT
    GPIO_InitStructure.GPIO_Pin = PAdf->LD;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
    GPIO_Init(PAdf->PORT, &GPIO_InitStructure);
}

void adf_t::PortInit(void) {
// Write to struct pins of ports
//====================  ADF1 - 1424 Mhz  =================
// HW
    Adf1424Mhz->RCC_CLK = ADF1_PORT_CLK;
    Adf1424Mhz->PORT = ADF1_PORT;
    Adf1424Mhz->SCK = ADF1_SCK;
    Adf1424Mhz->MOSI = ADF1_MOSI;
    Adf1424Mhz->CS = ADF1_CS;
    Adf1424Mhz->LD = ADF1_LD;
// SW
    Adf1424Mhz->Freq = ADF1_Freq;
    Adf1424Mhz->Int = ADF1_INTvalue;
    Adf1424Mhz->Frac = ADF1_FRACvalue;
    Adf1424Mhz->Mod = ADF1_MODvalue;
    Adf1424Mhz->Phase = ADF1_PHASEvalue;
    Adf1424Mhz->Lithera = ADF1_LitheraZero;
//====================  ADF2 - 248 MHz   =================
// HW
    Adf248MHz->RCC_CLK = ADF2_PORT_CLK;
    Adf248MHz->PORT = ADF2_PORT;
    Adf248MHz->SCK = ADF2_SCK;
    Adf248MHz->MOSI = ADF2_MOSI;
    Adf248MHz->CS = ADF2_CS;
    Adf248MHz->LD = ADF2_LD;
// SW
    Adf248MHz->Freq = ADF2_Freq;
    Adf248MHz->Int = ADF2_INTvalue;
    Adf248MHz->Frac = ADF2_FRACvalue;
    Adf248MHz->Mod = ADF2_MODvalue;
    Adf248MHz->Phase = ADF2_PHASEvalue;
    Adf248MHz->Lithera = ADF2_LitheraZero;
}

void adf_t::WriteToRegister(AdfDescriptions_t *PAdf, uint32_t ByteToWrite) {

    ADF_CS_LO(PAdf);
    Delay.us(9);                                                 // wait cs low
    for (uint8_t i=0; i<32; i++) {                               // start send data
        if (ByteToWrite & 0x80000000) ADF_MOSI_HI(PAdf);
        else ADF_MOSI_LO(PAdf);
        ByteToWrite <<= 1;
        Delay.us(5);
        ADF_SCK_HI(PAdf);
        Delay.us(5);
        ADF_SCK_LO(PAdf);
    }
    Delay.us(99);
    ADF_CS_HI(PAdf);                                                // load enable
    Delay.us(99);
}
